package cn.cena.service.impl;

import cn.cena.constant.BaseConstant;
import cn.cena.domain.Meal;
import cn.cena.exception.GlobalException;
import cn.cena.mapper.MealMapper;
import cn.cena.service.IMealService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 * 套餐表 服务实现类
 * </p>
 *
 * @author yu
 * @since 2021-11-22
 */
@Service
@Transactional(rollbackFor = Error.class)
public class MealServiceImpl extends ServiceImpl<MealMapper, Meal> implements IMealService {

    @Autowired
    private MealMapper mealMapper;

    /**
     * 保存Login和Meal关系表
     * @param loginId
     * @param mealId
     */
    @Override
    public void saveRelationLogin2Meal(Long loginId, Long mealId) {

        // 判断参数
        if(StringUtils.isEmpty(loginId)){
            throw new GlobalException("参数不能为空");
        }
        if(StringUtils.isEmpty(mealId)){
            throw new GlobalException("参数不能为空");
        }
        
        // 设置过期时间
        Date expireDate = DateUtils.addDays(new Date(), 7);

        mealMapper.saveRelationLogin2Meal(loginId, mealId, expireDate, BaseConstant.Meal.STATE_PAY_WAIT);

    }
}
